package com.fingersoft.feature.sensitiveword

import android.content.Context
import com.fingersoft.common.ICallback
import com.fingersoft.feature.sensitiveword.SensitiveWord.sensitive_REPLACE_list
import com.fingersoft.feature.sensitiveword.SensitiveWord.sensitive_SHIELD_list
import com.fingersoft.feature.sensitiveword.dao.SensitiveWordDaoUtils.Companion.instance
import com.fingersoft.feature.sensitiveword.dao.bean.SensitiveWordBean
import com.fingersoft.im.api.UserAgent
import com.fingersoft.im.api.base.BaseModelCallback2
import com.fingersoft.im.utils.AppUtils
import com.fingersoft.im.utils.JSONUtils
import com.google.gson.Gson
import com.lzy.okgo.OkGo
import okhttp3.Call
import okhttp3.Response
import org.json.JSONArray
import org.json.JSONException
import java.util.*

class SealSensitiveWordContext : ISensitiveWordContext {
    override fun getSensitiveWord(context: Context, path: String, params: String, callback: ICallback<*>) {
        OkGo.post(AppUtils.getApiUrl(path, AppUtils.J_ECODE))
            .headers("Emp-Info", JSONUtils.toJsonString(UserAgent(context, AppUtils.getTokenInfo().userToken, AppUtils.getTokenInfo().did)))
            .upJson(params)
            .execute(object : BaseModelCallback2<GetSensitiveWordResponse?>(GetSensitiveWordResponse::class.java) {
                override fun onSuccess(getsensitiveWordResponse: GetSensitiveWordResponse?, call: Call, response: Response) {
                    super.onSuccess(getsensitiveWordResponse, call, response)
                    if (AppUtils.showApiSucceedErrorToast(getsensitiveWordResponse)) {
                        callback.onError()
                        return
                    }
                    try {
                        if (getsensitiveWordResponse!!.data != null) {
                            val gson = Gson()
                            val jsonStr = gson.toJson(getsensitiveWordResponse.data)
                            var result: JSONArray? = null
                            try {
                                result = JSONArray(jsonStr)
                            } catch (e: JSONException) {
                                e.printStackTrace()
                            }
                            if (result != null && result.length() != 0) {
                                val list: MutableList<SensitiveWordBean?> = ArrayList()
                                for (i in 0 until result.length()) {
                                    var application: SensitiveWordBean? = null
                                    try {
                                        application = gson.fromJson(result[i].toString(), SensitiveWordBean::class.java)
                                    } catch (e: JSONException) {
                                        e.printStackTrace()
                                    }
                                    list.add(application)
                                }
                                instance.saveSensitiveWords(list.filterNotNull())
                            }
                        }
                        com.fingersoft.feature.sensitiveword.dao.AppUtils.saveSensitiveTime(getsensitiveWordResponse.timestamp)
                        val shieldSensitiveWords = instance.getSHIELDSensitiveWord() ?: listOf()
                        val replaceSensitiveWords = instance.getREPLACESensitiveWord() ?: listOf()
                        (sensitive_SHIELD_list as MutableList).addAll(shieldSensitiveWords.map { it.toSensitiveWordInfo() })
                        (sensitive_REPLACE_list as MutableList).addAll(replaceSensitiveWords.map { it.toSensitiveWordInfo() })
                        callback.onSuccess()
                    } catch (e: Exception) {
                        callback.onError(e.message)
                    }
                }

                override fun onError(call: Call, response: Response, e: Exception) {
                    super.onError(call, response, e)
                    callback.onError(e.message)
                }

            })
    }
}
